<?php

namespace Product\Controller;
use Common\Controller\HomebaseController; 
/**
 * 商品搜索页
 */
class ListController extends HomebaseController {
	protected  $page_num = 10;

    //进入商品详情页
	public function index() {
        $m_product = M("Products");
        $m_product_type = M("ProductType");
        $tid = I("get.tid");
        $keyword = I("get.keyword");

        $where = array();
        if(!empty($tid)){
            $where['tid'] = $tid;
            $cat = $m_product_type->where(array("id"=>$tid))->find();

            $this->assign("cat_title",$cat['title']);
        }else{
            $cat_list = $m_product_type->order("sort")->select();
            $this->assign('cat_list',$cat_list);
        }
        if(!empty($keyword)){
            $where['pname'] = array('like',"%$keyword%");
        }
        $page  = I('get.page');
        if(empty($page)){
            $page = 1;
        }
        $list = $m_product->where($where)->order("sort")->limit(($page-1)*$this->page_num , $this->page_num)->select();
        $count = $m_product->where($where)->count();
        $this->assign("page",$page);
        $this->assign("count",$count);
        $this->assign("list",$list);
        $this->assign("page_num",ceil($count/$this->page_num));
        $this->assign("url",U('product/list/index',array('tid'=>$tid,'keyword'=>$keyword)));
    	$this->display();
    }

}


